package adapter;

import java.util.ArrayList;

import com.adgroup.smartnote.R;
import com.androidquery.AQuery;

import JsonModel.UserLogo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class AdapterUserLogo {

	public class Adapter extends BaseAdapter {
		private Context context;
		private ArrayList<UserLogo> maleLogos;
		private ArrayList<UserLogo> femaleLogos;

		private int isMale;

		public void setIsMale(int isMale) {
			this.isMale = isMale;
		}

		public Adapter(Context context, ArrayList<UserLogo> logos, int isMale) {
			this.context = context;
			this.isMale = isMale;
			// male
			maleLogos = new ArrayList<UserLogo>();
			femaleLogos = new ArrayList<UserLogo>();
			for (UserLogo l : logos) {
				if (l.getGender().equalsIgnoreCase("1")) {
					maleLogos.add(l);
				} else {
					femaleLogos.add(l);
				}
			}
		}

		@Override
		public int getCount() {
			if (isMale == 1) {
				return maleLogos.size();
			} else {
				return femaleLogos.size();
			}
		}

		@Override
		public Object getItem(int arg0) {
			if (isMale == 1) {
				return maleLogos.get(arg0);
			} else {
				return femaleLogos.get(arg0);
			}
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			Holder holder;
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			if (arg1 == null) {
				holder = new Holder();
				arg1 = inflater.inflate(R.layout.itemuserlogo, null);
				holder.iv = (ImageView) arg1
						.findViewById(R.id.imageViewitemuserlogo);
				arg1.setTag(holder);
			} else {
				holder = (Holder) arg1.getTag();
			}
			AQuery aq = new AQuery(arg1);
			Bitmap placeholder = aq.getCachedImage(R.drawable.imageload);
			if (isMale == 1) {
				aq.id(holder.iv).image(maleLogos.get(arg0).getLink(), true,
						true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
			} else {
				aq.id(holder.iv).image(femaleLogos.get(arg0).getLink(), true,
						true, 0, 0, placeholder, AQuery.FADE_IN_NETWORK, 1.0f);
			}
			return arg1;
		}

	}

	public class Holder {
		ImageView iv;
	}
}
